package com.itheima.springbootwebtlias.controller;

import com.itheima.springbootwebtlias.pojo.*;
import com.itheima.springbootwebtlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //4.1 学员列表查询,条件分页查询接口
    @GetMapping
    public Result queryAllStudents(StuQueryParam stuQueryParam) {
        //1.接收请求参数：kv格式
        log.info("分页条件查询，请求参数：{}", stuQueryParam);
        //2.调用业务层Service功能方法，查询所有学员信息
        PageResult pageResult = studentService.page(stuQueryParam);
        //3.封装响应数据
        return Result.success(pageResult);
    }

    //4.3 添加学员,新增学生信息接口
    @PostMapping
    public Result saveStudent(@RequestBody Student student) {
        //1.接收请求参数： json
        log.info("接收到的参数：{}", student);
        //2.调用业务层Service功能方法，添加学员
        studentService.save(student);
        //3.封装响应数据
        return Result.success();
    }

    //4.4 根据ID查询学生 + 查询回显
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        //1.接收请求参数：path @PathVariable + 变量
        log.info("根据id查询员工信息，接收到的参数：{}", id);
        //2.调用业务层Service功能方法，查询回显
        Student student = studentService.queryById(id);
        //3.封装响应数据,返回结果
        return Result.success(student);
    }

    //4.5 修改学生信息接口
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        //1.接收请求参数：json
        log.info("修改更新员工信息，接收到的参数：{}", student);
        //2.调用业务层Service功能方法，修改员工信息
         studentService.update(student);
        //3.封装响应数据
        return Result.success();
    }

    //4.2 删除学生信息接口 + 批量删除
    @DeleteMapping("/{ids}")
    public Result removeById(@PathVariable List<Integer> ids){
        //1.接收请求参数：path @PathVariable + 集合类型变量
        log.info("批量删除员工，接收到的参数：{}",ids);
        //2.调用业务层Service功能方法，删除员工
        studentService.removeBatch(ids);
        //3.封装响应数据
        return Result.success();
    }
    //4.6 违纪处理,违纪次数和违纪扣分
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score){
        //1.接收请求参数：path @PathVariable + 集合类型变量
        log.info("违纪处理，接收到的参数：{},{}",id,score);
        //2.调用业务层Service功能方法，删除员工
        studentService.violation(id,score);
        //3.封装响应数据
        return Result.success();
    }
}
